package org.ceteca.explica.client.management;

import java.util.HashSet;

import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.management.DocumentOT;
import org.ceteca.explica.core.management.DocumentTypeOT;
import org.ceteca.explica.core.management.InterfaceManagement;
import org.ceteca.explica.core.management.SerialNumberOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

public class SerialNumberClient extends CommonClient {

	
	private Text id, description, prefix, value, documentType, version;
	private Button selectDocumentType;
	
	
	
	
	
	public SerialNumberClient() {
		super(SerialNumberOT.class,
				InterfaceManagement.EXPLICA_EDIT_SERIAL_NUMBER_GLADE_PATH,
				InterfaceManagement.EXPLICA_EDIT_SERIAL_NUMBER_DIALOG_NAME);
	}

	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();

		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfaceManagement.EXPLICA_ENTITY_DOCUMENTTYPE_ID,
				InterfaceManagement.EDIT_SERIAL_NUMBER_DOCUMENT_TYPE_WIDGETNAME, this,
				selectDocumentType));

	}

	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub

	}

	@Override
	public BaseVO getEntityData() {
		SerialNumberOT entity = new SerialNumberOT();
		if (this.entEditView != null) {
			this.getWidgets();
			entity.setDescription(description.getText());
			entity.setPrefix(prefix.getText());
			entity.setValue(Converter.getInstance().parseLong(value.getText()));
			entity.setDocumentType((DocumentTypeOT)documentType.getData());
			
			if (selectedEntity != null) {
				entity.setVersion(((SerialNumberOT) this.selectedEntity)
						.getVersion());
				entity.setIdSerialNumber(((SerialNumberOT) this.selectedEntity)
						.getIdSerialNumber());
			}
		}
		return entity;
	}

	@Override
	public void getWidgets() {
		int codError=0;
		if ((id = ((Text) this.getWidget(
				InterfaceManagement.EDIT_SERIAL_NUMBER_ID_WIDGETNAME))) == null) {
			codError = 1;
		}
		if ((description = ((Text) this.getWidget(
				InterfaceManagement.EDIT_SERIAL_NUMBER_DESCRIPTION_WIDGETNAME))) == null) {
			codError = 2;
		}
		
		if ((version = ((Text) this.getWidget(
				InterfaceManagement.EDIT_SERIAL_NUMBER_VERSION_WIDGETNAME))) == null) {
			codError = 3;
		}
		
		if ((prefix = ((Text) this.getWidget(
				InterfaceManagement.EDIT_SERIAL_NUMBER_PREFIX_WIDGETNAME))) == null) {
			codError = 6;
		}
		if ((value = ((Text) this.getWidget(
				InterfaceManagement.EDIT_SERIAL_NUMBER_VALUE_WIDGETNAME))) == null) {
			codError = 4;
		}
		if ((selectDocumentType = ((Button) this.getWidget(
				InterfaceManagement.EDIT_SERIAL_NUMBER_SELECT_DOCUMENT_TYPE_BUTTON_NAME))) == null) {
			codError = 5;
		}
		if ((documentType = ((Text) this.getWidget(
				InterfaceManagement.EDIT_SERIAL_NUMBER_DOCUMENT_TYPE_WIDGETNAME))) == null) {
			codError = 4;
		}
		
		if (codError!=0){
			System.out.println("Error #"+codError);
			this.fireExceptionAndExit("Error getting widget: "+codError);
		}

	}

	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setEditionMode(boolean enabled) {
			getWidgets();
			id.setEnabled(false);
			version.setEnabled(false);
			value.setEnabled(enabled);
			description.setEnabled(enabled);
			if (id.getText().equals("0")|| id.getText().isEmpty()){
				selectDocumentType.setEnabled(enabled);
				prefix.setEnabled(enabled);
				documentType.setEnabled(enabled);
				
			}else{
				selectDocumentType.setEnabled(false);
				prefix.setEnabled(false);
				documentType.setEnabled(false);
			}
				


	}

	@Override
	public void setWidgetsValues(BaseVO entity) {
			SerialNumberOT ent= (SerialNumberOT)entity;
			id.setText(Converter.getInstance().toString(ent.getIdSerialNumber()));
			description.setText(Converter.getInstance().toString(ent.getDescription()));
			prefix.setText(Converter.getInstance().toString(ent.getPrefix()));
			value.setText(Converter.getInstance().toString(ent.getValue()));
			version.setText(Converter.getInstance().toString(ent.getVersion()));
			// Selection buttons
			if (ent.getDocumentType()!= null) {
				documentType.setText(ent.getDocumentType().getDescription());
				documentType.setData(ent.getDocumentType());
			}

	}

	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((SerialNumberOT) selectedEntity);
		}
	
	}
}
